package com.party.notify.notifyPush.activity;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.party.core.model.activity.Activity;
import com.party.core.model.member.MemberAct;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.message.Message;
import com.party.core.model.message.MessageTag;
import com.party.core.model.notify.TargetTemplate;
import com.party.core.model.order.OrderStatus;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IPackageCountService;
import com.party.notify.notifyPush.servce.INotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by wei.li\
 *
 * @date 2017/8/9 0009
 * @time 10:20
 */
@Service
public abstract class INotifyActivitySendService {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private INotifyService notifyService;

    @Autowired
    private IPackageCountService packageCountService;

    @Autowired
    private IMemberGroupService memberGroupService;

    private static final String ACTIVE_PUSH = "activePush";

    /**
     * 推送
     * @param targetId 目标
     * @param authorId 发送者
     * @param targetTemplate 模板
     */
    public boolean send(String targetId, String authorId, TargetTemplate targetTemplate, String way){
        List<MemberAct> list = this.getList(targetId);
        boolean isCanFlag = packageCountService.checkSpareNum(targetTemplate, list.size());
        if (isCanFlag) {
            for (MemberAct memberAct : list) {
                // send
                this.toPush(memberAct, targetId, authorId, targetTemplate, way);
            }
        }
        return isCanFlag;
    }


    /**
     * 推送
     * @param memberAct 报名信息
     * @param targetId 目标编号
     * @param authorId 发送者
     * @param targetTemplate 模板
     */
    public void toPush(MemberAct memberAct,String targetId, String authorId, TargetTemplate targetTemplate, String way){
        Activity activity = activityService.get(memberAct.getActId());
        MemberGroup member = memberGroupService.getPartner(activity.getMember());

        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", memberAct.getName());
        constant.put("{title}", activity.getTitle());
        constant.put("{payment}", memberAct.getPayment());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("relId", memberAct.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("orderStatus", OrderStatus.ORDER_STATUS_HAVE_PAID.getCode());
        constant.put("createBy", memberAct.getCreateBy());
        constant.put("targetId", Strings.nullToEmpty(targetId));
        constant.put("senderId", Strings.nullToEmpty(authorId));
        constant.put("way", way);
        constant.put("{partner}", Strings.nullToEmpty(member.getRealname()));
        constant.put("templateId", targetTemplate.getId());
        notifyService.push(ACTIVE_PUSH, memberAct.getMobile(), memberAct.getCreateBy(), constant, targetTemplate.getTemplate());
    }


    /**
     * 获取发送数据
     * @param activityId 活动编号
     * @return 发送数据
     */
    public abstract List<MemberAct> getList(String activityId);
}
